package com.obas.integration.common.service.impl;

import java.util.Date;

import org.springframework.util.StopWatch;

import com.ease.common.util.DateFormatUtil;
import com.ease.common.util.Detect;
import com.ease.common.vo.MapParameter;
import com.ease.document.freemarker.FreeMarkerUtil;
import com.ease.integration.ftp.FtpTemplate;
import com.ease.jdbc.oracle.SqlLoaderControlCallback;
import com.ease.jdbc.oracle.SqlLoaderTemplate;
import com.ease.jdbc.oracle.vo.SqlLoaderParam;
import com.obas.integration.aaa.service.impl.AaaIntegrationFileServiceImpl;
import com.obas.integration.common.service.AbstractIntegrationFileService;

//如果表中有索引的话，是不能指定direct=TRUE的，除非使用skip_index_maintenance=TRUE，
//这个就是在导入的时候忽略索引，所以在数据导入完毕以后，查看索引的状态应该都是无效的，
//需要重建之,如下SQL：select  * from dba_indexes where table_name='?' ;
//alter  index index_name rebuild ;
public abstract class AbstractIntegrationFileServiceImpl extends Detect implements AbstractIntegrationFileService {

	protected boolean skipIndexMaintenance = false;

	protected String remoteDirectory = "/";

	protected String localDirectory;

	protected FtpTemplate ftpTemplate;

	protected SqlLoaderTemplate sqlLoaderTemplate;

	protected String sqlLoad(String fileName, final String controlTemplateName, SqlLoaderControlCallback sqlLoaderControlCallback) {

		SqlLoaderParam sqlLoaderParam = new SqlLoaderParam();
		//
		sqlLoaderParam.setDataFileFolder(localDirectory);
		sqlLoaderParam.setDataFileName(fileName);
		//

		String fileNamePrefix = fileName.indexOf('.') > 0 ? fileName.substring(0, fileName.lastIndexOf('0')) : fileName;

		sqlLoaderParam.setCtlFileFolder(localDirectory);
		sqlLoaderParam.setCtlFileName(fileNamePrefix + ".ctl");
		//
		sqlLoaderParam.setLogFileFolder(localDirectory);
		sqlLoaderParam.setLogFileName(fileNamePrefix + ".log");
		//
		return sqlLoaderTemplate.sqlldr(sqlLoaderParam, sqlLoaderControlCallback);
	}

	protected void load(String fileName, final Date batchTimestamp, final String controlTemplateName) {

		StopWatch stopWatch = new StopWatch();
		stopWatch.start();

		boolean downloadResult = ftpTemplate.download(remoteDirectory, fileName, localDirectory);
		if (false == downloadResult) {
			// TODO: audit
			System.out.println("audit: " + fileName + "not found");
			return;
		}

		String returnMessage = this.sqlLoad(fileName, controlTemplateName, new SqlLoaderControlCallback() {

			public String control() {
				MapParameter mapParameter = new MapParameter();
				mapParameter.put("batchTimestamp", DateFormatUtil.toDatetimeString(batchTimestamp));
				return FreeMarkerUtil.processFile(AaaIntegrationFileServiceImpl.class, controlTemplateName, mapParameter);
			}

		});

		ftpTemplate.remove(remoteDirectory, fileName);

		stopWatch.stop();

		// TODO:auditLog
		System.out.println(stopWatch.getLastTaskTimeMillis() / 1000 + "'s :" + returnMessage);
	}

	/**
	 * @return the ftpTemplate
	 */
	public FtpTemplate getFtpTemplate() {
		return ftpTemplate;
	}

	/**
	 * @param ftpTemplate
	 *            the ftpTemplate to set
	 */
	public void setFtpTemplate(FtpTemplate ftpTemplate) {
		this.ftpTemplate = ftpTemplate;
	}

	/**
	 * @return the sqlLoaderTemplate
	 */
	public SqlLoaderTemplate getSqlLoaderTemplate() {
		return sqlLoaderTemplate;
	}

	/**
	 * @param sqlLoaderTemplate
	 *            the sqlLoaderTemplate to set
	 */
	public void setSqlLoaderTemplate(SqlLoaderTemplate sqlLoaderTemplate) {
		this.sqlLoaderTemplate = sqlLoaderTemplate;
	}

	/**
	 * @return the remoteDirectory
	 */
	public String getRemoteDirectory() {
		return remoteDirectory;
	}

	/**
	 * @param remoteDirectory
	 *            the remoteDirectory to set
	 */
	public void setRemoteDirectory(String remoteDirectory) {
		this.remoteDirectory = remoteDirectory;
	}

	/**
	 * @return the localDirectory
	 */
	public String getLocalDirectory() {
		return localDirectory;
	}

	/**
	 * @param localDirectory
	 *            the localDirectory to set
	 */
	public void setLocalDirectory(String localDirectory) {
		this.localDirectory = localDirectory;
	}

	/**
	 * @return the skipIndexMaintenance
	 */
	public boolean isSkipIndexMaintenance() {
		return skipIndexMaintenance;
	}

	/**
	 * @param skipIndexMaintenance
	 *            the skipIndexMaintenance to set
	 */
	public void setSkipIndexMaintenance(boolean skipIndexMaintenance) {
		this.skipIndexMaintenance = skipIndexMaintenance;
	}
}
